﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplicationPlatform
{
    public partial class FrmMDIParent : Form
    {
        public FrmMDIParent()
        {
            InitializeComponent();
            initMenu();
        }

        /// <summary>
        /// 初始化菜单
        /// </summary>
        private void initMenu()
        {
            //菜单样子：
            //1.基础数据：2.系统管理
            //1.1 合格证颜色信息查询
            //2.1 用户维护

            for (int i = 0; i < 2; i++)
            {
                System.Windows.Forms.ToolStripMenuItem root = new ToolStripMenuItem();
                root.Name = "menu" + i;
                root.Text = i == 0 ? "基础数据" : "系统管理";

                DataTable dt_Leaf = initMenuChildData(i);
                for (int j = 0; j < dt_Leaf.Rows.Count; j++)
                {
                    System.Windows.Forms.ToolStripMenuItem leaf = new ToolStripMenuItem();
                    leaf.Name = "menu" + dt_Leaf.Rows[j]["menuid"].ToString().Trim();
                    leaf.Text = dt_Leaf.Rows[j]["menuname"].ToString().Trim();

                    if (dt_Leaf.Rows[j]["formname"].ToString().Trim() != "")
                    {
                        leaf.Tag = dt_Leaf.Rows[j]["formname"].ToString().Trim();
                        leaf.Click += new EventHandler(leaf_Click);
                    }
                    root.DropDownItems.Add(leaf);
                }

                this.menuStrip1.Items.Add(root);


            }
        }

        void leaf_Click(object sender, EventArgs e)
        {
            string formName = ((ToolStripMenuItem)sender).Tag.ToString();

            CreateFormInstance(formName);
        }

        private void CreateFormInstance(string formName)
        {
            bool flag = false;
            //遍历主窗口上的所有子菜单 
            for (int i = 0; i < this.MdiChildren.Length; i++)
            {
                //如果所点的窗口被打开则重新激活 
                if (this.MdiChildren[i].Tag.ToString().ToLower() == formName.ToLower())
                {
                    this.MdiChildren[i].Activate();
                    this.MdiChildren[i].Show();
                    this.MdiChildren[i].WindowState = FormWindowState.Normal;
                    flag = true;
                    break;
                }
            }

            if (!flag)
            {
                Assembly asm = Assembly.Load("WindowsFormsApplicationPlatform");//程序集名 
                object frmObj = asm.CreateInstance("WindowsFormsApplicationPlatform." + formName);//程序集+form的类名。 
                Form frms = (Form)frmObj;
                //tag属性要重新写一次，否则在第二次的时候取不到。原因还不清楚。有知道的望告知。   
                frms.Tag = formName;
                frms.MdiParent = this;
                frms.Show();
            }
        }

        private DataTable initMenuChildData(int i)
        {
            DataTable menuTable = new DataTable();
            menuTable.Columns.Add("menuid");
            menuTable.Columns.Add("menuname");
            menuTable.Columns.Add("formname");

            if (i == 0)
            {
                DataRow newRow = menuTable.NewRow();
                newRow["menuid"] = "1";
                newRow["menuname"] = "合格证颜色信息查询";
                newRow["formname"] = "FrmMDIParentChild1";

                menuTable.Rows.Add(newRow);
            }
            if (i == 1)
            {
                DataRow newRow = menuTable.NewRow();
                newRow["menuid"] = "2";
                newRow["menuname"] = "用户维护";
                newRow["formname"] = "FrmMDIParentChild2";

                menuTable.Rows.Add(newRow);
            }

            return menuTable;

        }
    }
}
